<?php include('includes/start_session.php') ?>
<?php include('includes/login_required.php') ?>
<?php
	include('db-api/txt-db-api.php');
	include('includes/course_info.php');
	
	$found = FindCourse($_GET['id'], $name);
	$isProf = false;
	$isStud = false;
	if($found == true)
	{
		$isProf = IsUserProf($_GET['id'], $name);
		$isStud = false;
		if($isProf == false)
			$isStud = IsUserStudent($_GET['id'], $name);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
	<title>e-class agenda</title>
	
	<link href="styles/site.css" rel="stylesheet" type="text/css" />
	<link href="styles/zipped.css" rel="stylesheet" type="text/css" />
	
	<script type="text/javascript" src="scripts/scripts.js"></script>
</head>
<body onload="same_height()">
	<?php include('includes/header.php') ?>
	<div id="content" class="content_padding">
		<?php
			if($found == false)
				echo '<h3>Το μάθημα δε βρέθηκε</h3>';
			else if($isProf == false && $isStud == false)
				echo '<h3>Δεν είστε εξουσιοδοτημένοι να δείτε την αντζέντα!</h3>';
			else
			{
				echo '<h3>' . $name . '</h3>';
				echo '<div class="center_align"><b>Αντζέντα μαθήματος</b></div>';
				echo '<br />';
				if($isProf == true)
					echo '<a class="course_anchor" href="add_agenda.php?id=' . $_GET['id'] . '">προσθήκη νέου γεγονότος</a>';
		?>
		<div class="right_align">
			<?php
				echo '<a href="course.php?id=' . $_GET['id'] . '" class="course_anchor">Επιστροφή</a>';
			?>
		</div>
		<?php
			$monthnames = array(
				'Ιανουάριος', 'Φεβρουάριος',
				'Μάρτιος', 'Απρίλιος',
				'Μάιος', 'Ιούνιος',
				'Ιούλιος', 'Αύγουστος',
				'Σεπτέμβριος', 'Οκτώβριος',
				'Νοέμβριος', 'Δεκέμβριος'
			);

			echo '<div class="center_align">';
			if(isset($_GET['month']))
				$month = $_GET['month'];
			else
				$month = date('n', time());
			if(isset($_GET['year']))
				$year = $_GET['year'];
			else
				$year = date('Y', time());
			if($month == 1)
			{
				$prevmonth = 12;
				$prevyear = $year - 1;
			}
			else
			{
				$prevmonth = $month - 1;
				$prevyear = $year;
			}
			if($month == 12)
			{
				$nextmonth = 1;
				$nextyear = $year + 1;
			}
			else
			{
				$nextmonth = $month + 1;
				$nextyear = $year;
			}
			echo '<a href="agenda.php?id=' . $_GET['id'] . 		'&month=' . $prevmonth . '&year=' . $prevyear . '">&lt;	&lt;</a>';
			echo '&nbsp;&nbsp;' . $monthnames[$month - 1] . '&nbsp;&nbsp;' . $year . '&nbsp;&nbsp;';
			echo '<a href="agenda.php?id=' . $_GET['id'] . 		'&month=' . $nextmonth . '&year=' . $nextyear . '">&gt;	&gt;</a>';
			echo '</div>';
		?>
		<ul class="agenda_list">
			<?php
				$db = new Database('Eclass');
				$rs = $db->executeQuery(
					'SELECT StartDate, EndDate, Title, Description
					 FROM Agenda
					 WHERE CourseId = ' . $_GET['id'] .
					' ORDER BY StartDate DESC'
				);
				$i = 0;
				while($rs->next())
				{
					list($start_date, $end_date, $title, $desc) = $rs->getCurrentValues();
					$curstartdate = date_parse($start_date);
					$curenddate = date_parse($end_date);
					$cur1 = '1-' . $curstartdate['month'] . '-' . $curstartdate['year'];
					$cur2 = '1-' . $curenddate['month'] . '-' . $curenddate['year'];
					$now = '1-' . $month . '-' . $year;
					if(strtotime($cur1) > strtotime($now) || strtotime($now) > strtotime($cur2))
						continue;
					echo '<li>';
					echo '<div class="event">';
					echo '<h4 class="event_title" onclick="hide(\'' . $i . '\')">' . $title . '</h4>';
					echo '<div class="event_stuff" id="' . $i . '">';
					
					if(strtotime($start_date) <= strtotime('now') && strtotime('now') <= strtotime($end_date))
						$css_class = 'current_event';
					else if(strtotime($start_date) <= strtotime('now'))
						$css_class = 'passed';
					else
						$css_class = 'not_passed';
					echo '<span class="minor_title">Ημερομηνία έναρξης: </span>';
					echo '<span class="' . $css_class . '">' . $start_date . '</span><br />';
					echo '<span class="minor_title">Ημερομηνία λήξης: </span>';
					echo '<span class="' . $css_class . '">' . $end_date . '</span><br />';
					echo '<span class="minor_title">Περιγραφή:<br /><br /></span>';
					echo '<span class="event_description">' . $desc . '</span>';
					
					if($isProf == true)
					{
						echo '<br /><br />';
						echo '<a href="#" class="anchor">επεξεργασία</a>';
						echo '&nbsp;&nbsp;&nbsp;';
						echo '<a href="#" class="anchor">διαγραφή</a>';
					}
					
					echo '</div>';
					echo '</div>';
					echo '</li>';

					$i++;
				}
				if($i == 0)
					echo 'Δε βρέθηκαν γεγονότα στην αντζέντα του μαθήματος';
			?>
		</ul>
		<?php
			if($i > 0)
			{
		?>
		<div class="right_align">
			<a class="course_anchor" href="#">Επιστροφή στην κορυφή</a>
		</div>
		<?php }} ?>
	</div>
	<?php include('includes/footer.php') ?>
</body>
</html>
